﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Maticsoft.Web.Admin.Feedback
{
    public partial class SurveyList : PageBase
    {
        string _Key = string.Empty;
        BLL.Survey bll = new BLL.Survey();
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("feedback", "read");

            if (!string.IsNullOrEmpty(Request.QueryString["key"]))
            {
                _Key = Request.QueryString["key"];
            }

            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            string strWhere = "IsActive=1";
            if (!string.IsNullOrEmpty(_Key))
            {
                strWhere += " and SurveyName like '%" + _Key + "%'";
            }

            int recordCount = bll.GetRecordCount(strWhere);
            AspNetPager1.RecordCount = recordCount;
            DataSet ds = bll.GetListByPage(strWhere, "SurveyId desc", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            Repeater1.DataSource = ds.Tables[0].DefaultView;
            Repeater1.DataBind();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("SurveyList.aspx?key=" + txtKey.Value.Trim());
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Check("feedback", "del");

            string ids = txtIds.Value;
            if (!string.IsNullOrWhiteSpace(ids))
            {
                //bll.DeleteByIds(ids);
            }
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindData();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            editT.Visible = true;
            txtTitle.Text = "";
            hidSurveyId.Value = "";
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "ModifyItem")
            {
                editT.Visible = true;
                Model.Survey soModel = bll.GetModel(int.Parse(e.CommandArgument.ToString()));
                txtTitle.Text = soModel.SurveyName;
                hidSurveyId.Value = soModel.SurveyId.ToString();
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (hidSurveyId.Value != "")
            {
                Model.Survey soModel = bll.GetModel(int.Parse(hidSurveyId.Value));
                soModel.SurveyName = txtTitle.Text.Trim();
                soModel.UpdateTime = DateTime.Now;
                bll.Update(soModel);
            }
            else
            {
                Model.Survey soModel = new Model.Survey();
                soModel.SurveyName = txtTitle.Text.Trim();
                soModel.CreateTime = DateTime.Now;
                soModel.UpdateTime = DateTime.Now;
                soModel.IsActive = 1;
                bll.Add(soModel);
            }

            BindData();
            editT.Visible = false;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            editT.Visible = false;
            txtTitle.Text = "";
            hidSurveyId.Value = "";
        }
    }
}